﻿/* 
 * 作 者: 槐尧 
 * 创建时间: 2012/10/8 16:12:00 
 * 说明:  
 */

using System.Collections;
using System.Collections.Generic;

public class DictionaryUnity 
{
    public static object GetSafeKeyValue(Dictionary<string, object> dic, string key)
    {
        object result = null;
        if (!dic.TryGetValue(key, out result))
        {
            //Debug.LogWarning("do not contain key " + key);
        }
        
        return result;
    }

    /// <summary>
    /// 获取整形，如果没有该字段返回-1
    /// </summary>
    /// <param name="dic">Dictionary</param>
    /// <param name="key">键</param>
    /// <returns></returns>
    public static int GetSafeIntValue(Dictionary<string, object> dic, string key)
    {
        object result = GetSafeKeyValue(dic,key);
        if (result != null)
        {
            return (int)result;
        }
        else
        {
            return -1;
        }
    }

    /// <summary>
    /// 获取浮点型，如果没有该字段返回-1
    /// </summary>
    /// <param name="dic">Dictionary</param>
    /// <param name="key">键</param>
    /// <returns></returns>
    public static float GetSafeFloatValue(Dictionary<string, object> dic, string key)
    {
        object result = GetSafeKeyValue(dic, key);
        if (result == null)
        {
            return 0f;
        }
        return GetFloat(result);
    }

    /// <summary>
    /// 获取字符串，如果没有该字段返回-1
    /// </summary>
    /// <param name="dic">Dictionary</param>
    /// <param name="key">键</param>
    /// <returns></returns>
    public static string GetSafeStringValue(Dictionary<string, object> dic, string key)
    {
        object result = GetSafeKeyValue(dic, key);
        if (result != null)
        {
            return result as string;
        }
        else
        {
            return "";
        }
    }

    /// <summary>
    /// 获取List，如果没有该字段返回-1
    /// </summary>
    /// <param name="dic">Dictionary</param>
    /// <param name="key">键</param>
    /// <returns></returns>
    public static List<object> GetSafeListValue(Dictionary<string, object> dic, string key)
    {
        object result = GetSafeKeyValue(dic, key);
        List<object> tag = null;
        tag = result as List<object>;
        if (result != null && tag == null)
        {
           // Debug.LogError("key is not list type");
        }

        return tag;
    }

    /// <summary>
    /// 获取Dictionary，如果没有该字段返回-1
    /// </summary>
    /// <param name="dic">Dictionary</param>
    /// <param name="key">键</param>
    /// <returns></returns>
    public static Dictionary<string, object> GetSafeDictionaryValue(Dictionary<string, object> dic, string key)
    {
        object result = GetSafeKeyValue(dic, key);

        Dictionary<string, object> tag = null;
        tag = result as Dictionary<string, object>;
        if (result != null && tag == null)
        {
            //Debug.LogError("key is not Dictionary type");
        }

        return tag;
    }

    /// <summary>
    /// 获取浮点型
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static float GetFloat(object value)
    {
        float result = 0;
        if (value is int)
        {
            result = (float)(int)value;
        }
        else if (value is double)
        {
            result = (float)(double)value;
        }
        else if (value is float)
        {
            result = (float)value;
        }
        else
        {
            //Debug.Log("version  is not float or int   " + value.GetType().ToString());
        }
        return result;
    }
}